package com.htj.erp.vo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class EsayuiTree {

	private String id; // 节点的 id，它对于加载远程数据很重要。
	private String parentId; //
	private String text; // 要显示的节点文本。
	private String state = "open"; // 节点状态，'open' 或 'closed'，默认是 'open'。当设置为
									// 'closed' 时，该节点有子节点，并且将从远程站点加载它们。
	private String iconCls;
	private Boolean isFolder;
	private Boolean checked; // 指示节点是否被选中。
	private Map<String, Object> attributes; // 给一个节点添加的自定义属性。
	private List<EsayuiTree> children; // 定义了一些子节点的节点数组。

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getIconCls() {
		if (isFolder != null && isFolder && StringUtils.isBlank(iconCls)
				&& (children == null || children.size() == 0)) {
			return "kd-icon-folder";
		}else if(null != isFolder && !isFolder){
			return "kd-icon-file";
		}
		return iconCls;
	}

	public void setIconCls(String iconCls) {
		this.iconCls = iconCls;
	}

	public void setIsFolder(Boolean isFolder) {
		this.isFolder = isFolder;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public Map<String, Object> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Object> attributes) {
		this.attributes = attributes;
	}

	public void addAttribut(String key, Object value) {
		if (this.attributes == null) {
			this.attributes = new HashMap<>();
		}
		this.attributes.put(key, value);
	}

	public void addAttributes(Map<String, Object> map) {
		if (map != null) {
			if (this.attributes == null) {
				this.attributes = new HashMap<>();
			}
			this.attributes.putAll(map);
		}
	}

	public List<EsayuiTree> getChildren() {
		return children;
	}

	public void setChildren(List<EsayuiTree> children) {
		this.children = children;
	}

	public void addChild(EsayuiTree child) {
		if (this.children == null) {
			this.children = new ArrayList<>();
		}
		this.children.add(child);
	}

	public static List<EsayuiTree> listToTree(List<EsayuiTree> list) {
		List<EsayuiTree> tree = new ArrayList<>();
		for (EsayuiTree vo1 : list) {
			int k = 0;
			for (EsayuiTree vo2 : list) {
				if (vo1.getId().equals(vo2.getParentId())) {
					vo1.addChild(vo2);
					k++;
				}
			}
			
			if(k == 0){
				vo1.setIsFolder(Boolean.FALSE);
				vo1.setState("open");
			}
			
			if (StringUtils.isBlank(vo1.getParentId())) {
				tree.add(vo1);
			}
		}
		return tree;
	}
}
